package database;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 程式資訊摘要 : 擷取Solr之回傳結果
 * 類別名稱:
 * 程式內容說明:
 * 版本資訊:1.0
 * 程式設計人員姓名: 彭紹峻
 */
public class Solr_sim {

	/**
	 * 找出與智產點相關智產點之資訊
	 * 
	 * @param s
	 *            :欲查詢之字串
	 * @return 
	 *         回傳值為與查詢字串的hashmap,key值為智產編號_verison,值為另一hashmap,可以由key=Score取出智產點分數
	 *         ,key=Group_id取出智產點所屬智產圖id,key=Type_id取出智產點所屬智產類型
	 */

	public static HashMap<String, HashMap<String, String>> getSimilarList(
			String s) {

		String oneLine = null;
		boolean isFinish = false;// 擷取資料的停止點判斷
		ArrayList<String> aid = new ArrayList<String>();// 儲存相似智產點的ID
		ArrayList<String> verison = new ArrayList<String>();// 儲存相似智產點的版本
		ArrayList<String> temp_id = new ArrayList<String>();// solr中對應分數的虛擬ID
		ArrayList<String> type_id = new ArrayList<String>();// //儲存相似智產點的類型
		ArrayList<String> group_id = new ArrayList<String>();// 儲存相似智產點所屬之智產圖
		HashMap<String, String> virtualScore = new HashMap<String, String>();// 儲存虛擬ID和分數的對應

		HashMap<String, HashMap<String, String>> fin = new HashMap<String, HashMap<String, String>>();
		String name = null;
		String w_name="50";
		String w_version="50";
		String w_externalid="50";
		String w_shortdescription="50";
		String w_specificattributes="50";
		String w_layer="50";
		String w_state="50";
		String w_atname="50";
		
		
		
		try {
			name = URLEncoder.encode(s, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//String url = "http://140.122.184.132:8983/solr/smartasset/select?q="
				//+ name + "&wt=json&indent=true&debugQuery=true"; //URL
		String url ="http://140.122.184.132:8983/solr/smartasset/select?q="+ name +"&wt=json&indent=true&defType=edismax&debugQuery=true&qf=NAME^"+w_name+"+VERSION^"+ w_version+"+EXTERNALID^"+w_externalid+"+SHORTDESCRIPTION^"+w_shortdescription+"+SPECIFICATTRIBUTES^"+w_specificattributes+"+LAYER^"+w_layer+"+STATE^"+ w_state+"+AT.NAME^"+w_atname;
		//System.out.println(url);
		try {

			URL url_address = new URL(url);
			
			BufferedReader br = new BufferedReader(new InputStreamReader(
					url_address.openStream(), "UTF-8"));

			while ((oneLine = br.readLine()) != null) {
				
				//System.out.println(oneLine);
				// 資訊擷取
				if (oneLine.contains('"'+"EXTERNALID"+'"')) {
					oneLine = oneLine.substring(
							oneLine.indexOf("EXTERNALID") + 13,
							oneLine.length() - 2);

					aid.add(oneLine);
				}

				if (oneLine.contains('"'+"VERSION"+'"'+":"+'"')) {
					oneLine = oneLine.substring(
							oneLine.indexOf("VERSION") + 10,
							oneLine.length() - 2);

					verison.add(oneLine);
				}

				if (oneLine.contains('"' + "ID" + '"')) {
					oneLine = oneLine.substring(oneLine.indexOf("ID") + 5,
							oneLine.length() - 2);

					temp_id.add(oneLine);
				}

				if (oneLine.contains('"' + "QParser" + '"')) {

					isFinish = false;

				}

				if (oneLine.contains('"' + "ASSETTYPEID" + '"')) {
					oneLine = oneLine.substring(
							oneLine.indexOf('"' + "ASSETTYPEID" + '"') + 14,
							oneLine.length() - 1);

					type_id.add(oneLine);
				}

				if (oneLine.contains('"' + "RESOURCEGROUP" + '"')) {
					oneLine = oneLine.substring(
							oneLine.indexOf('"' + "RESOURCEGROUP" + '"') + 17,
							oneLine.length() - 2);

					group_id.add(oneLine);
				}

				if (isFinish) {

					oneLine = oneLine.substring(7, oneLine.indexOf(" (MATCH)"));
					String id = oneLine
							.substring(0, oneLine.indexOf('"' + ":"));
					String score = oneLine.substring(oneLine.indexOf(":") + 4,
							oneLine.length() - 2);

					virtualScore.put(id, score);

				}
				if (oneLine.contains('"' + "explain" + '"')) {

					isFinish = true;

				}
			}
			HashMap<String, String> temp = new HashMap<String, String>();// 暫存
			for (int i = 0; i < aid.size(); i++) {

				temp = new HashMap<String, String>();
				temp.put("Score", (Double.parseDouble(virtualScore.get(temp_id.get(i)))/2.5)+"");
				temp.put("Group_id", group_id.get(i));
				temp.put("Type_id", type_id.get(i));
				fin.put(aid.get(i) + "_" + verison.get(i), temp);
				
			}
br.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return fin;// 回傳
	}
}
